﻿#region

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

#endregion

namespace VinhSon.Intranet.Models
{
    public static class ServerExtensions
    {
        public static string GetResource(this HttpServerUtilityBase httpServerUtilityBase,
                                         string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                if ("updatesuccessful".Equals(key,
                                             StringComparison.CurrentCultureIgnoreCase))
                    return "Update Successful.";
                if ("errorwhenupdated".Equals(key,
                                             StringComparison.CurrentCultureIgnoreCase))
                    return "Error when updated.";
                if ("informationhasbeenused".Equals(key,
                                                   StringComparison.CurrentCultureIgnoreCase))
                    return "Information has been in used.";
                if ("informationhasexists".Equals(key,
                                                 StringComparison.CurrentCultureIgnoreCase))
                    return "Information has exists.";
                if ("user.add.username.exists".Equals(key,
                                                 StringComparison.CurrentCultureIgnoreCase))
                    return "Username/Email has exists.";
                //if ("".Equals(key,
                //                             StringComparison.CurrentCultureIgnoreCase))
                //    return "";
                return "";
            }
            return String.Empty;
        }

        private static string FormatResult(this HttpServerUtilityBase httpServerUtilityBase,
                                           UpdateResult result)
        {
            JArray array = new JArray
                               {
                                       new JValue(httpServerUtilityBase.GetResource(result.KeyLanguage))
                               };
            if (result.KeyLanguages != null)
                foreach (string key in result.KeyLanguages)
                    array.Add(new JValue(httpServerUtilityBase.GetResource(key)));
            return array.ToString(Formatting.None);
        }

        public static dynamic FormatResultToJson(this HttpServerUtilityBase httpServerUtilityBase,
                                                 UpdateResult result)
        {
            return new
                       {
                           lv = result.State,
                           msg = FormatResult(httpServerUtilityBase,
                                              result)
                       };
        }

        private static string GetResource(IEnumerable<KeyValuePair<string, ModelState>> modelState)
        {
            JObject obj = new JObject();
            foreach (KeyValuePair<string, ModelState> item in modelState)
                if (item.Value.Errors.Count > 0)
                    obj.Add(item.Key,
                            ErrorsToString(item.Value.Errors));
            return obj.ToString(Formatting.None);
        }

        private static string ErrorsToString(IEnumerable<ModelError> errors)
        {
            string first = "";
            StringBuilder stringBuilder = new StringBuilder();
            foreach (ModelError error in errors)
            {
                stringBuilder.AppendFormat("{0}{1}",
                                           first,
                                           error.ErrorMessage);
                if (string.IsNullOrEmpty(first))
                    first = ", ";
            }
            return stringBuilder.ToString();
        }

        public static dynamic FormatResultToJson(this HttpServerUtilityBase httpserverutilitybase,
                                                 ModelStateDictionary modelState)
        {
            return new
                       {
                           lv = 4,
                           forms = GetResource(modelState)
                       };
        }
    }
}
